﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class OwnerTransferDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private static readonly IOwnerTransferDA transferDA = DataAccess.CreateOwnerTransferDA();
        private static readonly IOwnerTransferDetailDA transferDetailDA = DataAccess.CreateOwnerTransferDetailDA();

        public OwnerTransferDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public OwnerTransferDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OwnerTransferDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteOwnerTransferDetail(List<string> transferIDs, List<string> LineIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (transferIDs.Count != LineIDs.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F5300_005", this.pubParasInfo.UserLanguage);
                }
                List<OwnerTransferDetailInfo> transferDetailInfos = new List<OwnerTransferDetailInfo>();
                OwnerTransferDetailInfo transferDetailInfo = null;
                for (i = 0; i < transferIDs.Count; i++)
                {
                    transferDetailInfo = transferDetailDA.GetOwnerTransferDetailByID(dataBase, tran, transferIDs[i], LineIDs[i], this.dataBaseID[0]);
                    transferDetailInfos.Add(transferDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (transferDetailDA.DeleteOwnerTransferDetail(dataBase, tran, transferIDs, LineIDs, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < transferIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<OwnerTransferDetailInfo>(dataBase, tran, transferDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TRANSFER_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + transferIDs[i] + " , " + LineIDs[i] + " ; ";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public OwnerTransferDetailInfo GetOwnerTransferDetailInfoByID(string transferID, string LineID)
        {
            return transferDetailDA.GetOwnerTransferDetailByID(transferID, LineID, this.dataBaseID[0]);
        }

        public string InsertOwnerTransferDetailInfo(OwnerTransferDetailInfo transferDetailInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string transferLine = string.Empty;
            try
            {
                transferDetailInfo.UpdatedBy = transferDetailInfo.CreatedBy;
                transferDetailInfo.CreatedDate = DateTime.Now;
                transferDetailInfo.UpdatedDate = DateTime.Now;
                if (!skuDA.CheckSkuIDUnique(dataBase, tran, transferDetailInfo.FromOwnerID, transferDetailInfo.FromSkuID, this.dataBaseID[0]))
                {
                    throw new Exception("原货品代码" + transferDetailInfo.FromSkuID + " 不存在！");
                }
                if (!skuDA.CheckSkuIDUnique(dataBase, tran, transferDetailInfo.ToOwnerID, transferDetailInfo.ToSkuID, this.dataBaseID[0]))
                {
                    throw new Exception("至货品代码" + transferDetailInfo.ToSkuID + " 不存在！");
                }
                if (transferDetailDA.InsertOwnerTransferDetail(dataBase, tran, transferDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OwnerTransferDetailInfo>(null, transferDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TRANSFER_DETAIL", this.dataBaseID[0]);
                    transferLine = transferDetailInfo.LineID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return transferLine;
        }

        public string UpdateOwnerTransferDatail(OwnerTransferDetailInfo transferDetailInfo)
        {
            string LineID = string.Empty;
            try
            {
                if (!skuDA.CheckSkuIDUnique(transferDetailInfo.FromOwnerID, transferDetailInfo.FromSkuID, this.dataBaseID[0]))
                {
                    throw new Exception("原货品代码" + transferDetailInfo.FromSkuID + " 不存在！");
                }
                if (!skuDA.CheckSkuIDUnique(transferDetailInfo.ToOwnerID, transferDetailInfo.ToSkuID, this.dataBaseID[0]))
                {
                    throw new Exception("至货品代码" + transferDetailInfo.ToSkuID + " 不存在！");
                }
                OwnerTransferDetailInfo oldOwnerTransferDetailInfo = transferDetailDA.GetOwnerTransferDetailByID(transferDetailInfo.TransferID, transferDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                if (transferDetailDA.UpdateOwnerTransferDetail(transferDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OwnerTransferDetailInfo>(oldOwnerTransferDetailInfo, transferDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "TRANSFER_DETAIL", this.dataBaseID[0]);
                    LineID = transferDetailInfo.TransferID + " , " + transferDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }
    }
}

